<template>
    <div>
        <el-dialog
            :title="!form.id ? '新增' : '修改'"
            :visible.sync="visible"
            :close-on-click-modal="false">

            <div class="table_form_wrap">
                <el-form ref="form" :model="form" :rules="rules" @keyup.enter.native="handleSubmit()" label-width="120px">
                    <el-form-item label="地点名称" prop="addressName">
                        <el-input v-model="form.addressName" placeholder="地址名称" style="width: 80%;"></el-input>
                    </el-form-item>

                    <el-form-item label="地址类型" prop="sendStatus">
                        <el-radio-group v-model="form.type" size="small">
                            <el-radio :label="2" border>收货</el-radio>
                            <el-radio :label="1" border>发货</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="默认地址" prop="receiveStatus">
                        <el-radio-group v-model="form.isDefault" size="small">
                            <el-radio :label="true" border>是</el-radio>
                            <el-radio :label="false" border>否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="收发货人姓名" prop="name">
                        <el-input v-model="form.name" placeholder="收发货人姓名" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="收货人电话" prop="phone">
                        <el-input v-model="form.phone" placeholder="收货人电话" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="省" prop="province">
                        <el-input v-model="form.province" placeholder="省" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="市" prop="city">
                        <el-input v-model="form.city" placeholder="市" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="区" prop="region">
                        <el-input v-model="form.region" placeholder="区" style="width: 80%;"></el-input>
                    </el-form-item>
                    <el-form-item label="详细地址" prop="detailAddress">
                        <el-input v-model="form.detailAddress" placeholder="详细地址" style="width: 80%;"></el-input>
                    </el-form-item>
                </el-form>
            </div>

            <div slot="footer" class="dialog-footer">
                <el-button @click="visible = false">取 消</el-button>
                <el-button @click="handleSubmit()" type="primary" :submitLoading="submitLoading">提 交</el-button>
            </div>

        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            visible: false,
            submitLoading: false,

            form: {
                addressName: null,
                isDefault: false,
                type: 1,
                name: null,
                phone: null,
                province: null,
                city: null,
                region: null,
                detailAddress: null
            },
            rules: {
                addressName: [
                    { required: true, message: '地点名称不能为空', trigger: 'blur' }
                ],
                name: [
                    { required: true, message: '收发货人姓名不能为空', trigger: 'blur' }
                ],
                phone: [
                    { required: true, message: '收货人电话不能为空', trigger: 'blur' }
                ],
                province: [
                    { required: true, message: '省份不能为空', trigger: 'blur' }
                ],
                city: [
                    { required: true, message: '城市不能为空', trigger: 'blur' }
                ],
                region: [
                    { required: true, message: '区不能为空', trigger: 'blur' }
                ],
                detailAddress: [
                    { required: true, message: '详细地址不能为空', trigger: 'blur' }
                ]
            }
        }
    },

    methods: {
        init(id) {
            const refs = this.$refs
            // 情况表单数据
            this.form.id = id || null
            this.visible = true

            this.$nextTick(() => {
                refs['form'].resetFields()
                if(this.form.id) {
                    this.getInfo(this.form.id)
                }
            })
        },

        /**
         * 详细信息
         */
        async getInfo(id) {
            if (id) {
                const res = await this.$http({
                    url: '/mall/malldeliveryaddress/info/' + id,
                    method: 'GET'
                })
                this.form = Object.assign({}, res.data)
            }
        },

        /**
         * 提交表单
         */
        async handleSubmit() {
            const refs = this.$refs
            this.submitLoading = true
            const valid = await refs['form'].validate().catch(() => {})
            if(valid) {
                const res = await this.$http({
                    url: `/mall/malldeliveryaddress/${!this.form.id ? 'save' : 'update'}`,
                    method: 'POST',
                    data: this.form
                })
                if (this.ResultMessage(res)) {
                    this.$emit('refreshDataList')
                    this.submitLoading = false
                    this.visible = false
                } else {
                    this.submitLoading = false
                }
            } else {
                this.submitLoading = false
            }
        }

    }
};
</script>

<style lang="scss" scoped></style>
